﻿Imports System.Data
Imports System.Data.SqlClient
Imports System.Web.Configuration
Imports CrystalDecisions.CrystalReports.Engine
Imports CrystalDecisions.Shared
Partial Class Reports_report_stp_auth_group3
    Inherits System.Web.UI.Page
    Private report As New CrystalDecisions.CrystalReports.Engine.ReportDocument

    Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
        CrystalReportViewer1.ReportSource = report
        report.PrintOptions.PaperOrientation = PaperOrientation.Landscape
        CrystalReportViewer1.PrintMode = CrystalDecisions.Web.PrintMode.ActiveX

    End Sub

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim dw As report_stp_auth_groupTableAdapters.USP_List_stp_auth_group2TableAdapter
        Dim dw2 As report_stp_auth_groupTableAdapters.USP_List_stp_auth_getTableAdapter
        Dim dt As DataTable
        Dim dt2 As DataTable
        Dim iDepartmentCode As Integer
        Dim iAuthCode As Integer
        Dim iMainCode As Integer
        Dim iMenuCode As Integer
        Dim iGroupCode As Integer
        Dim iStaff As Integer
        Dim iColumn As Integer
        Dim totalColumn As Integer
        Dim iCount As Integer = 1
        Try

            'get Querystring
            'iStaff = Request.QueryString("staff_code")
            iStaff = Session("staff_code")
            iDepartmentCode = Request.QueryString("department_code")
            iGroupCode = Request.QueryString("group_code")
            iAuthCode = Request.QueryString("auth_group_status")
            iMainCode = Request.QueryString("main_system_code")
            iMenuCode = Request.QueryString("menu_system_code")


            dw = New report_stp_auth_groupTableAdapters.USP_List_stp_auth_group2TableAdapter
            dw2 = New report_stp_auth_groupTableAdapters.USP_List_stp_auth_getTableAdapter
            'Retrieve data from DB & collect to datatable
            dt = dw.GetData(iStaff, iGroupCode, iDepartmentCode, iMainCode, iMenuCode, iAuthCode)

            dt2 = dw2.GetData()


            'Get no. of authen column
            iColumn = dt2.Rows.Count
            'check 
            If (dt.Rows.Count > 0) Then
                'Create report engine
                'Dim report As New CrystalDecisions.CrystalReports.Engine.ReportDocument


                'map report parth
                report.Load(Server.MapPath("report_stp_auth_group.rpt"))
                totalColumn = 12
         
                'set data table to report
                report.SetDataSource(dt)

                Dim remainColumn As Integer
                For Each dr As DataRow In dt2.Rows
                    report.SetParameterValue("param" + iCount.ToString(), dr("type_name"))
                    iCount += 1
                Next

                remainColumn = totalColumn - iCount + 1

                For index As Integer = 1 To remainColumn
                    report.SetParameterValue("param" + (iCount).ToString, "")
                    iCount += 1
                Next


                'hidden line
                hiddenLine(report, iColumn)

                'Show report
                'CrystalReportViewer1.ReportSource = report
            Else
                ' hide invalid message
                CrystalReportViewer1.Visible = False

                GenericHelper.ShowAlert(Me, "ไม่พบข้อมูล")
                Dim strscript As String = "<script language=javascript>window.top.close();</script>"
                If (Not Page.IsStartupScriptRegistered("clientScript")) Then
                    Page.RegisterStartupScript("clientScript", strscript)
                End If
                ' TODO:
                ' Redirect to pervious page
            End If
        Catch ex As AccessViolationException
            ' hide invalid message
            CrystalReportViewer1.Visible = False

            GenericHelper.ShowAlert(Me, ex.Message)
            Dim strscript As String = "<script language=javascript>window.top.close();</script>"
            If (Not Page.IsStartupScriptRegistered("clientScript")) Then
                Page.RegisterStartupScript("clientScript", strscript)
            End If
        Finally
            If Not (dt Is Nothing) Then
                dt.Dispose()
            End If
            If Not (dw Is Nothing) Then
                dw.Dispose()
            End If
        End Try
    End Sub

    Private Sub hiddenLine(ByVal report As CrystalDecisions.CrystalReports.Engine.ReportDocument, ByVal Column As Integer)
        Dim Line1, Line2, Line5, Line3 As LineObject
        Dim txtHeader, text18 As TextObject

        For i As Integer = Column + 1 To 12
            setNoLine("Line" + CStr(i) + "_M", report)
            setNoLine("Line" + CStr(i) + "_T", report)
            setNoLine("Line" + CStr(i) + "_B", report)
        Next

        'setNoLine("Line9", report)

        For j As Integer = 1 To Column - 1
            setNoLine("Line" + CStr(j) + "_T", report)
        Next

        If Column = 2 Then

            'top line header
            txtHeader = report.ReportDefinition.ReportObjects("txtHeader")
            txtHeader.Width = 1622

            text18 = report.ReportDefinition.ReportObjects("Text18")
            text18.Width = 4903

            Line2 = report.ReportDefinition.ReportObjects("Line2")
            Line2.Right = 8160

            Line1 = report.ReportDefinition.ReportObjects("Line1")
            Line1.Right = 8160

            Line5 = report.ReportDefinition.ReportObjects("Line5")
            Line5.Right = 8160

        ElseIf Column = 3 Then

            'setNoLine("Line1_T", report)
            'setNoLine("Line2_T", report)
            'top line header
            txtHeader = report.ReportDefinition.ReportObjects("txtHeader")
            txtHeader.Width = 2420

            text18 = report.ReportDefinition.ReportObjects("Text18")
            text18.Width = 5773

            Line2 = report.ReportDefinition.ReportObjects("Line2")
            Line2.Right = 9030

            Line1 = report.ReportDefinition.ReportObjects("Line1")
            Line1.Right = 9030

            Line5 = report.ReportDefinition.ReportObjects("Line5")
            Line5.Right = 9030
        ElseIf Column = 4 Then

            'top line header
            txtHeader = report.ReportDefinition.ReportObjects("txtHeader")
            txtHeader.Width = 3320

            text18 = report.ReportDefinition.ReportObjects("Text18")
            text18.Width = 6643

            Line2 = report.ReportDefinition.ReportObjects("Line2")
            Line2.Right = 9885

            Line1 = report.ReportDefinition.ReportObjects("Line1")
            Line1.Right = 9885

            Line5 = report.ReportDefinition.ReportObjects("Line5")
            Line5.Right = 9885

        ElseIf Column = 5 Then
 
            'top line header
            txtHeader = report.ReportDefinition.ReportObjects("txtHeader")
            txtHeader.Width = 4262

            text18 = report.ReportDefinition.ReportObjects("Text18")
            text18.Width = 7535

            Line2 = report.ReportDefinition.ReportObjects("Line2")
            Line2.Right = 10793

            Line1 = report.ReportDefinition.ReportObjects("Line1")
            Line1.Right = 10793

            Line5 = report.ReportDefinition.ReportObjects("Line5")
            Line5.Right = 10793

        ElseIf Column = 6 Then
 
            'top line header
            txtHeader = report.ReportDefinition.ReportObjects("txtHeader")
            txtHeader.Width = 5102

            text18 = report.ReportDefinition.ReportObjects("Text18")
            text18.Width = 8398

            Line2 = report.ReportDefinition.ReportObjects("Line2")
            Line2.Right = 11648


            Line1 = report.ReportDefinition.ReportObjects("Line1")
            Line1.Right = 11648

            Line5 = report.ReportDefinition.ReportObjects("Line5")
            Line5.Right = 11648
        ElseIf Column = 7 Then

            'top line header
            txtHeader = report.ReportDefinition.ReportObjects("txtHeader")
            txtHeader.Width = 5957

            text18 = report.ReportDefinition.ReportObjects("Text18")
            text18.Width = 9260

            Line2 = report.ReportDefinition.ReportObjects("Line2")
            Line2.Right = 12503

            Line1 = report.ReportDefinition.ReportObjects("Line1")
            Line1.Right = 12503

            Line5 = report.ReportDefinition.ReportObjects("Line5")
            Line5.Right = 12503
        ElseIf Column = 8 Then

            'top line header
            txtHeader = report.ReportDefinition.ReportObjects("txtHeader")
            txtHeader.Width = 6767

            text18 = report.ReportDefinition.ReportObjects("Text18")
            text18.Width = 10078

            Line2 = report.ReportDefinition.ReportObjects("Line2")
            Line2.Right = 13335

            Line1 = report.ReportDefinition.ReportObjects("Line1")
            Line1.Right = 13335

            Line5 = report.ReportDefinition.ReportObjects("Line5")
            Line5.Right = 13335
        ElseIf Column = 9 Then

            'top line header
            txtHeader = report.ReportDefinition.ReportObjects("txtHeader")
            txtHeader.Width = 7652

            text18 = report.ReportDefinition.ReportObjects("Text18")
            text18.Width = 10955

            Line2 = report.ReportDefinition.ReportObjects("Line2")
            Line2.Right = 14190


            Line1 = report.ReportDefinition.ReportObjects("Line1")
            Line1.Right = 14190

            Line5 = report.ReportDefinition.ReportObjects("Line5")
            Line5.Right = 14190

        ElseIf Column = 10 Then

            'top line header
            txtHeader = report.ReportDefinition.ReportObjects("txtHeader")
            txtHeader.Width = 8507

            text18 = report.ReportDefinition.ReportObjects("Text18")
            text18.Width = 11840

            Line2 = report.ReportDefinition.ReportObjects("Line2")
            Line2.Right = 15083

            Line1 = report.ReportDefinition.ReportObjects("Line1")
            Line1.Right = 15083

            Line5 = report.ReportDefinition.ReportObjects("Line5")
            Line5.Right = 15083
        ElseIf Column = 11 Then

            'top line header
            txtHeader = report.ReportDefinition.ReportObjects("txtHeader")
            txtHeader.Width = 9457

            text18 = report.ReportDefinition.ReportObjects("Text18")
            text18.Width = 12710

            Line2 = report.ReportDefinition.ReportObjects("Line2")
            Line2.Right = 15935

            Line1 = report.ReportDefinition.ReportObjects("Line1")
            Line1.Right = 15935

            Line5 = report.ReportDefinition.ReportObjects("Line5")
            Line5.Right = 15935
        Else
           
        End If

    End Sub
 
    Private Sub setNoLine(ByVal LineName As String, ByVal rDoc As CrystalDecisions.CrystalReports.Engine.ReportDocument)
        Dim objLine As LineObject = CType(rDoc.ReportDefinition.ReportObjects(LineName), LineObject)
        objLine.LineStyle = CrystalDecisions.Shared.LineStyle.NoLine
    End Sub

    Protected Sub Page_Unload(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Unload
        If Not (CrystalReportViewer1 Is Nothing) Then
            CrystalReportViewer1.Dispose()
        End If
    End Sub

    Protected Sub CrystalReportViewer1_Unload(ByVal sender As Object, ByVal e As System.EventArgs) Handles CrystalReportViewer1.Unload
        If Not (report Is Nothing) Then
            report.Close()
            report.Dispose()
        End If
    End Sub

End Class
